const BASES: [char; 16] = [
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
];

fn parse_num_str_to_10(numstr: &str) -> u32 {
    let base = {
        let rev_base = numstr
            .chars()
            .rev()
            .skip(1)
            .take_while(|&c| c != '(')
            .collect::<String>();
        rev_base
            .chars()
            .rev()
            .collect::<String>()
            .parse::<u32>()
            .unwrap()
    };
    let num = numstr.chars().take_while(|&c| c != '(');
    num.fold(0, |acc, x| acc * base + char_to_10u32(x))
}

fn char_to_10u32(c: char) -> u32 {
    if c.is_numeric() {
        c.to_digit(10).unwrap()
    } else {
        c.to_ascii_lowercase() as u32 - 'a' as u32 + 10
    }
}

pub fn convert_base(num_str: &str, to_base: u32) -> String {
    let mut num = parse_num_str_to_10(num_str);
    let mut result = String::new();
    while num > 0 {
        let digit = num % to_base;
        num /= to_base;
        result.push(BASES[digit as usize]);
    }
    result.chars().rev().collect()
}
